package com.lph.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.lph.model.Cocs;
import com.lph.model.Files;
import com.lph.model.Msg;
import com.lph.service.BaseServiceI;
import com.lph.util.FileIOUtil;
@RequestMapping("/cc")
@Controller
public class CocsController extends BaseController<Cocs>{
	
	
	@Resource(name="cocsService")
	private BaseServiceI<Cocs> csbi;
	@Resource(name="filesService")
	private BaseServiceI<Files> fsi;
	@Override
	public List<Cocs> find(Cocs t) {
		return csbi.find(t);
	}
	@RequestMapping("/co.coc")
	public String cocs(){
		return "/cocs/index";
	}
	@RequestMapping("/add.coc")
	public String add(HttpServletRequest req) throws Exception{
		String vals=req.getParameter("vals");
		if(vals!=null){//防止乱码
			vals=new String(vals.getBytes("iso-8859-1"),"utf-8");
		}
		Cocs cc=JSON.parseObject(vals,Cocs.class);
		req.setAttribute("cc", cc);
		return "/cocs/addOrEdit";
	}
	@RequestMapping("/inserts.coc")
	@ResponseBody
	
	public Msg inserts(Cocs t,@RequestParam(required=false,value="photo") MultipartFile[] files) throws Exception{
		Msg m=csbi.insert(t);
		if(m.isIs()){
			List<Files> list=FileIOUtil.saveFile(files, "t_coc_coc", t.getId());
			if(list!=null){
				fsi.insert(list.get(0));
			}
		}
		return m;
		
	}
	@RequestMapping("/editt.coc")
	@ResponseBody
	public Msg edit(Cocs t) {
		return csbi.edit(t);
	}
	@RequestMapping("/removee/{id}.coc")
	@ResponseBody
	public Msg remove(@PathVariable(value="id")Integer id){
		return csbi.remove(id);
	}
	
	@RequestMapping("/batRomve.coc")
	@ResponseBody
	public Msg batRemove(String ids){
		return csbi.batchRemove(ids);
		
	}
	
	@RequestMapping("/code")
	public String code(){
		return "/cocs/code";
	}
}
